/*
* $Id: AddChangeView.cpp 161 2010-10-03 23:45:28Z volok-aleksej@yandex.ru $
*
* Copyright 2009 by Volokitin Alexey
* All rights reserved
*
*/
#include "Common/include.h"
#include "AddChangeView.h"
#include "Database/Tables/UserFieldsTable.h"
#include "Visual/Drawers/EditorDrawer/EditorStructs.h"

#pragma warning(disable: 4407)

#define CX_BUTTON		110
#define CY_BUTTON		30
#define DIST_BUTTON		10
#define SIZE_SCROLL		17
#define BUTTON_REGION	(CX_BUTTON + DIST_BUTTON)

#define SB_USER_UP			9
#define SB_USER_DOWN		10

CAddChangeView::CAddChangeView(CState* pState)
: CEmptyViewJournal(pState), m_editorDrawer(dynamic_cast<IWnd*>(this)), m_bVScroll(false),
m_nIdParameter(pState->m_nIdParameter), m_nTick(0), m_bSaveEnable(true){}
CAddChangeView::~CAddChangeView(){
	m_editorDrawer.Clear();
}

void CAddChangeView::PaintView(CDC* dc, CRect rect){
	CPen pen;
	pen.CreatePen(PS_SOLID, 1, RGB(0, 0, 0));
	dc->MoveTo(rect.TopLeft());
	dc->LineTo(CPoint(rect.right - 1, rect.top));
	dc->LineTo(CPoint(rect.right - 1, rect.bottom - 1));
	dc->LineTo(CPoint(rect.left - 1, rect.bottom - 1));
	pen.DeleteObject();
	CBrush brush;
	brush.CreateSolidBrush(GetSysColor(COLOR_3DFACE));
	for(int i = 0; i < 2; i++){
		dc->FillRect(CRect(rect.left + i*BUTTON_REGION, rect.top + 1, rect.left + DIST_BUTTON + i*BUTTON_REGION, rect.bottom - 1), &brush);
		dc->FillRect(CRect(rect.left + i*BUTTON_REGION + DIST_BUTTON, rect.top + 1,
			rect.left + (i+1)*BUTTON_REGION, rect.top + DIST_BUTTON), &brush);
		dc->FillRect(CRect(rect.left + i*BUTTON_REGION + DIST_BUTTON, rect.bottom - DIST_BUTTON,
			rect.left + (i+1)*BUTTON_REGION, rect.bottom - 1), &brush);
	}
	dc->FillRect(CRect(rect.left + 2*BUTTON_REGION, rect.top + 1, rect.right - 1, rect.bottom - 1), &brush);
	brush.DeleteObject();
}

void CAddChangeView::UpgradeScroll(){
	CRect rect;
	GetClientRect(rect);
	int sizeCyTable = rect.Size().cy - 2* DIST_BUTTON - CY_BUTTON;
	SCROLLINFO si;
	if(sizeCyTable < m_editorDrawer.GetHeightTable()){
		m_VScroll.ShowWindow(SW_SHOW);
		m_VScroll.GetScrollInfo(&si);
		si.nPage = sizeCyTable;
		si.nMin = 0;
		si.nMax = m_editorDrawer.GetHeightTable();
		m_VScroll.SetScrollInfo(&si);
		m_bVScroll = true;
		m_editorDrawer.SetSizeCx(rect.Size().cx - SIZE_SCROLL);
	}else{
		m_VScroll.ShowWindow(SW_HIDE);
		m_VScroll.GetScrollInfo(&si);
		si.nPos = 0;
		m_VScroll.SetScrollInfo(&si);
		m_bVScroll = false;
		m_editorDrawer.SetSizeCx(rect.Size().cx);
	}
	m_VScroll.GetScrollInfo(&si);
	m_editorDrawer.SetBeginPos(-si.nPos);
}

void CAddChangeView::GetCurValue(){}
void CAddChangeView::InitCurrentValue(){}

void CAddChangeView::EventHandler(){
	GetCurValue();
	Init();
}

BEGIN_MESSAGE_MAP(CAddChangeView, CEmptyViewJournal)
	ON_WM_CREATE()
	ON_WM_SIZE()
	ON_WM_PAINT()
	ON_WM_VSCROLL()
	ON_WM_LBUTTONDOWN()
	ON_WM_LBUTTONUP()
	ON_WM_RBUTTONDOWN()
	ON_WM_SYSKEYDOWN()
	ON_WM_KEYDOWN()
	ON_WM_SETCURSOR()
	ON_WM_MOUSEWHEEL()
	ON_WM_MOUSEMOVE()
	ON_COMMAND(BUTTON_CANCEL_ID, OnCancel)
END_MESSAGE_MAP()

int CAddChangeView::OnCreate(LPCREATESTRUCT lpCreateStruct){
	int ret = __super::OnCreate(lpCreateStruct);
	m_nIdParameter = m_pState->m_nIdParameter;
	m_aCurValues.clear();
	InitCurrentValue();
	Init();
	m_buttonSave.Create(ILanguageManager::GetLanguageManager()->GetString(SAVE_ID).c_str(),
		WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, CRect(), this, BUTTON_SAVE_ID);
	m_buttonSave.EnableWindow(m_bSaveEnable);
	m_buttonCancel.Create(ILanguageManager::GetLanguageManager()->GetString(CANCEL_ID).c_str(),
		WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, CRect(), this, BUTTON_CANCEL_ID);
	m_VScroll.Create(WS_CHILD | WS_VISIBLE | SBS_VERT, CRect(), this, VSCROLL_ID);
	return ret;
}
void CAddChangeView::OnSize(UINT nType, int cx, int cy){
	CRect rect;
	GetClientRect(rect);
	m_buttonSave.MoveWindow(CRect(rect.left + DIST_BUTTON, rect.bottom - CY_BUTTON - DIST_BUTTON,
		rect.left + BUTTON_REGION, rect.bottom - DIST_BUTTON), FALSE);
	m_buttonCancel.MoveWindow(CRect(rect.left + BUTTON_REGION + DIST_BUTTON,
		rect.bottom - 40, rect.left + 2*BUTTON_REGION, rect.bottom - DIST_BUTTON), FALSE);
	m_VScroll.MoveWindow(CRect(rect.right - SIZE_SCROLL, rect.top,
		rect.right, rect.bottom - CY_BUTTON - 2 * DIST_BUTTON), TRUE);
	__super::OnSize(nType, cx, cy);
}
void CAddChangeView::OnPaint(){
	UpgradeScroll();
	CPaintDC dc(this);
	CRect rect;
	GetClientRect(rect);
	PaintView(&dc, CRect(rect.left, rect.bottom - CY_BUTTON - 2 * DIST_BUTTON, rect.right, rect.bottom));
	CRect rectTable(rect.left, rect.top, rect.right, rect.bottom - CY_BUTTON - 2 * DIST_BUTTON);
	if(m_bVScroll)
		rectTable.right -= SIZE_SCROLL;
	m_editorDrawer.Paint(&dc, rectTable);
}

void CAddChangeView::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar){
	SCROLLINFO si;
	pScrollBar->GetScrollInfo(&si);
	if(nSBCode == SB_THUMBTRACK){
		si.nPos = si.nTrackPos;
	} else if(nSBCode == SB_LINEDOWN){
		si.nPos += 10;
	} else if(nSBCode == SB_LINEUP){
		si.nPos -= 10;
	} else if(nSBCode == SB_PAGEDOWN){
		si.nPos += si.nPage;
	} else if(nSBCode == SB_PAGEUP){
		si.nPos -= si.nPage;
	}else if(nSBCode == SB_USER_UP){
		si.nPos -= 30;
	}else if(nSBCode == SB_USER_DOWN){
		si.nPos += 30;
	}
	pScrollBar->SetScrollInfo(&si);
	m_VScroll.GetScrollInfo(&si);
	m_editorDrawer.SetBeginPos(-si.nPos);
	Invalidate(FALSE);
	__super::OnVScroll(nSBCode, nPos, pScrollBar);
}

afx_msg void CAddChangeView::OnCancel(){
	IWnd* pWnd = IVisualManager::GetVisualManager()->
		GetVisualComponent(ILanguageManager::GetLanguageManager()->GetString(JOURNAL_CAPTION_ID))->GetComponentFrame();
	pWnd->Post(UM_ITEM_MANIPULATION, Journal::NONE, 0);
}

void CAddChangeView::OnLButtonDown(UINT nFlags, CPoint point){
	CRect rect;
	GetClientRect(rect);
	rect.bottom -= CY_BUTTON + 2 * DIST_BUTTON;
	if(m_bVScroll)
		rect.right -= SIZE_SCROLL;
	if(rect.PtInRect(point)){
		m_editorDrawer.SelectItem(point);
		m_editorDrawer.ProcessMouse(WM_LBUTTONDOWN, point, 0);
		Invalidate(FALSE);
	}
	SetFocus();
}

void CAddChangeView::OnLButtonUp(UINT nFlags, CPoint point){
	CRect rect;
	GetClientRect(rect);
	rect.bottom -= CY_BUTTON + 2 * DIST_BUTTON;
	if(m_bVScroll)
		rect.right -= SIZE_SCROLL;
	if(rect.PtInRect(point)){
		m_editorDrawer.ProcessMouse(WM_LBUTTONUP, point, 0);
		Invalidate(FALSE);
	}
}

void CAddChangeView::OnRButtonDown(UINT nFlags, CPoint point){
	CRect rect;
	GetClientRect(rect);
	rect.bottom -= CY_BUTTON + 2 * DIST_BUTTON;
	if(m_bVScroll)
		rect.right -= SIZE_SCROLL;
	if(rect.PtInRect(point)){
		m_editorDrawer.SelectItem(point);
		m_editorDrawer.ProcessMouse(WM_RBUTTONDOWN, point, 0);
		Invalidate(FALSE);
	}
	SetFocus();
}

afx_msg void CAddChangeView::OnMouseMove(UINT nFlags, CPoint point){
	DWORD tick;
	tick = GetTickCount();
	if(m_nTick - tick > 20){
		m_editorDrawer.ProcessMouse(WM_MOUSEMOVE, point, nFlags);
		Invalidate(FALSE);
		m_nTick = tick;
	}
}

BOOL CAddChangeView::OnMouseWheel(UINT nFlags, short zDelta, CPoint point){
	CRect rect;
	GetWindowRect(rect);
	rect.bottom -= CY_BUTTON + 2 * DIST_BUTTON;
	if(m_bVScroll)
		rect.right -= SIZE_SCROLL;
	if(rect.PtInRect(point)){
		CPoint pos = point - rect.TopLeft();
		if(!m_editorDrawer.ProcessMouse(WM_MOUSEWHEEL, pos, zDelta) && m_bVScroll)
			OnVScroll(zDelta > 0 ? SB_USER_UP : SB_USER_DOWN, 0, &m_VScroll);
		Invalidate(FALSE);
		return TRUE;
	}
	return __super::OnMouseWheel(nFlags, zDelta, point);
}

void CAddChangeView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags){
	byte sCtrl = HIBYTE(GetKeyState(VK_CONTROL));
	byte sAlt = (nFlags & 0x2000) >> 13;
	byte sShift = HIBYTE(GetKeyState(VK_SHIFT));
	m_editorDrawer.ProcessKey(nChar, ((sCtrl&1)<<2)|((sAlt&1)<<1)|(sShift & 1));
	__super::OnKeyDown(nChar, nRepCnt, nFlags);
	Invalidate(FALSE);
}

BOOL CAddChangeView::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message){
	CPoint pos;
	CRect rect;
	GetCursorPos(&pos);
	GetWindowRect(rect);
	pos = pos - rect.TopLeft();
	if(!m_editorDrawer.ProcessMouse(WM_SETCURSOR, pos, 0))
		SetCursor(LoadCursor(NULL, IDC_ARROW));
	return __super::OnSetCursor(pWnd, nHitTest, message);
}

void CAddChangeView::OnSysKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags){
	byte sCtrl = HIBYTE(GetKeyState(VK_CONTROL));
	byte sAlt = nFlags & 0x3000 >> 13;
	byte sShift = HIBYTE(GetKeyState(VK_SHIFT));
	m_editorDrawer.ProcessKey(nChar, (sCtrl<<2)|(sAlt<<1)|sShift);
	__super::OnSysKeyDown(nChar, nRepCnt, nFlags);
	Invalidate(FALSE);
}

void CAddChangeView::EnableSaveButton(bool bEnable){
	if(IsWindow(m_buttonSave.m_hWnd))
		m_buttonSave.EnableWindow(bEnable);
	m_bSaveEnable = bEnable;
}


